package cn.tedu.mall.seckill.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.pojo.seckill.vo.SeckillSpuDetailSimpleVO;
import cn.tedu.mall.pojo.seckill.vo.SeckillSpuVO;
import cn.tedu.mall.seckill.service.ISeckillSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangyiping
 * @date 2024/1/19
 */
// 什么时候使用Controller注解 答：文件上传下载的时候 直接使用response返回数据流 byte[]
@RestController
@RequestMapping("/seckill/spu")
@Api(tags = "订单秒杀功能之查询spu")
public class SeckillSpuController {

    @Autowired
    private ISeckillSpuService seckillSpuService;

    @GetMapping("/list")
    @ApiOperation(value = "获取预热好的spu列表")
    @PreAuthorize("permitAll()")
    public JsonResult<JsonPage<SeckillSpuVO>> getSpuList() {
        JsonPage<SeckillSpuVO> jsonPage = seckillSpuService.listSeckillSpus(null, null);
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/{spuId}")
    @ApiOperation(value = "根据spuId获取单个spu")
    @PreAuthorize("permitAll()")
    public JsonResult<SeckillSpuVO> getSpuById(@PathVariable("spuId") Long spuId) {
        SeckillSpuVO seckillSpuVO = seckillSpuService.getSeckillSpu(spuId);
        return JsonResult.ok(seckillSpuVO);
    }

    @GetMapping("/{spuId}/detail")
    @ApiOperation(value = "根据spuId获取spu详情")
    @PreAuthorize("permitAll()")
    public JsonResult<SeckillSpuDetailSimpleVO> getSpuDetailById(@PathVariable("spuId") Long spuId) {
        SeckillSpuDetailSimpleVO seckillSpuDetail = seckillSpuService.getSeckillSpuDetail(spuId);
        return JsonResult.ok(seckillSpuDetail);
    }

}
